<?php

namespace App\Model;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ChannelStatistics extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'channel_statistics';
    public $timestamps = false;
    protected $fillable = ['channel_id', 'register_user', 'new_recharge_user', 'new_recharge_price', 'grand_register', 'grand_recharge_user', 'grand_recharge', 'arpu', 'day_recharge_price', 'day_recharge_num', 'created_date'];

    public function getGrandRechargeAttribute($value)
    {
        if (!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 2);
    }

    public function getArpuAttribute($value)
    {
        if (!$value) {
            $value = 0;
        } else {
            $value /= 100;
        }
        return number_format($value, 6);
    }

    public function getNewRechargePriceAttribute($value)
    {
        if (!$value) return 0;
        return $value / 100;
    }

    public function getDayRechargePriceAttribute($value)
    {
        if (!$value) return 0;
        return $value / 100;
    }

    public function getList(Request $request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $channel_id = $request->input('channel_id', '');
        $begin_date = $request->input('begin_date');
        $end_date = $request->input('end_date');

        $query = new self();

        if ($channel_id) $query = $query->where('channel_id', $channel_id);
        if ($begin_date && $end_date) $query = $query->whereBetween('created_date', [$begin_date, $end_date]);
        if ($begin_date && !$end_date) $query = $query->where('created_date', '>=', $begin_date);
        if (!$begin_date && $end_date) $query = $query->where('created_date', '<=', $end_date);

        return $query->orderBy('id', 'DESC')->paginate($limit, ['*'], 'page', $page);
    }

    public function getAdminList($request)
    {
        $limit = $request['limit'] ?? 10;
        $page = $request['page'] ?? 1;
        $channel_id = $request['channel_id'];
        $begin_date = $request['begin_date'];
        $end_date = $request['end_date'];
        $query = new self();
        if ($channel_id) $query = $query->where('channel_id', $channel_id);
        if ($begin_date && $end_date) $query = $query->whereBetween('created_date', [$begin_date, $end_date]);
        if ($begin_date && !$end_date) $query = $query->where('created_date', '>=', $begin_date);
        if (!$begin_date && $end_date) $query = $query->where('created_date', '<=', $end_date);

        $res = $query->orderBy('id', 'DESC')->paginate($limit, ['*'], 'page', $page);
        return $this->objToArray($res);
    }

    //获取渠道下用户当日消耗
    public function getChannelDiamond($channel, $date)
    {
        $start_time = strtotime($date);
        $end_time = $start_time + 86399;
        $sql = "select user_id , COALESCE(SUM(ABS(diamond_value)),0) diamond_value  from zb_finance_diamond_stream  where user_id in (select user_id from zb_user_register_log
                        where channel_id = '" . $channel . "') and diamond_value < 0 and change_timestamp > " . $start_time . " and change_timestamp < " . $end_time . "  group by user_id ";
        $res = $this->objToArray(DB::connection('api')->select($sql));
        $diamond = array_column($res, 'diamond_value');
        return array_sum($diamond);
    }

}
